<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  To change this template use File | Settings | File Templates.
--%>
<html>
<head>
    <title>ajax_learning</title>
    <script type="text/javascript">
        function createXMLHttpRequest(){//  创建异步对象
            try {
                return new XMLHttpRequest();
            }catch (e) {
                try {
                   return new ActiveXObject("Msxml2.XMLHTTP");
                }catch (e) {
                    try {
                        return new ActiveXObject("Microsoft.XMLHTTP");
                    }catch (e) {
                        alert("error!!!!!!!!!!!!!!");
                    }
                }
            }
        }
        window.onload = function () {
            var btn = document.getElementById("btn");
            btn.onclick = function () {
                // 四步操作，得到响应在，回显；
                xmlHttp = createXMLHttpRequest();  // 01
                xmlHttp.open("get","<c:url value="/AjaxServlet"/>", true);  // 02
                xmlHttp.send(null);  // 03
                xmlHttp.onreadystatechange = function() {
                    if(xmlHttp.readyState === 4 && xmlHttp.status === 200){
                        var h1 = document.getElementById("h1");
                        var text = xmlHttp.responseText;
                        h1.innerHTML = text;// get the text;
                    }
                }
            }
        };
    </script>
</head>
<body>
<button id="btn">click here</button>
<h1 id="h1"></h1>
</body>
</html>
